<template>
  

    <el-container class="home-container">
        <!-- 头部区域 -->
  <el-header >  
       <div>
           <img src="../assets/lazy.png" height="60px" alt="">
           <span>华伟后台管理系统</span>
       </div>
      <el-button type="info" @click="logout" >退出 </el-button>  
      
        </el-header>

  <el-container>

      <!-- 侧边栏 -->
    <el-aside :width="isCollapse ?'64px':'200px'">
      <!-- 折叠展开 -->
     <div class="toggle-button" @click="toggleCollapse"><a href="javascript:;">|||</a></div>
  <el-menu background-color="#333744" text-color="#fff"   active-text-color="#409EFF" :unique-opened="true" 

  :collapse="isCollapse" 
  :collapse-transition='false' 
  :router="true"
  :default-active="activePath"
  >
  <!-- router开启路由模式 -->
<!-- 一级菜单 -->
<!-- 他的这个index值接收字符串 所以绑定的时候也要用字符串 加一个空字符就阔以解决数值和空字符拼接就阔以得到字符串 -->
      <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id" >
          <!--一级菜单模板区域  -->
        <template slot="title">
            <!-- 图标 -->
          <i :class="iconsObj[item.id]"></i>
          <!-- 文本 -->
          <span>{{item.authName}}</span>
        </template>


         <!-- 二级菜单 -->
          <el-menu-item :index="'/'+subitem.path" v-for="subitem in item.children" :key="subitem.id"
          @click="saveNavState('/'+subitem.path)" >

          <template slot="title">
            <!-- 图标 -->
            <i class="el-icon-menu"></i>
            <!-- 文本 -->
              <span>{{subitem.authName}}</span>

    </template>
          </el-menu-item>
  
      </el-submenu>
    
    </el-menu>

    </el-aside>



    <!-- 主体区 -->

      <el-main>

        <router-view></router-view><!--这里是路由占位符-->
      </el-main>

  </el-container>

        </el-container>


</template>

<script>
export default{

  data(){
    return{
      // 左侧数据菜单数据
           menulist:[],

           iconsObj:{
             '125':'iconfont icon-morentouxiang',
             '103':'iconfont icon-tijikongjian',
             '101':'iconfont icon-shangpin',
             '102':'iconfont icon-danju-tianchong',
             '145':'iconfont icon-baobiao',

           },
           isCollapse:false,
           activePath:'',//被激活的链接地址 这样就能保证选择那个选项 那个就一直亮着
    }
  },
      created(){//生命周期函数
        // 创建函数
           this.getMenuList()
           this.activePath =window.sessionStorage.getItem('activePath')
      },

    methods:{
        logout(){
            window.sessionStorage.clear();
            this.$router.push('/login')
        },
        // async这样阔以简化 获取的东西
      async  getMenuList(){

       const {data:res} =  await this.$http.get('menus')
      //  console.log( "下面是从接口中获取的date数据：");
      //  console.log(res);
            if(res.meta.status !==200)
            {
              return this.$message.error(res.meta.msg);
            }
            this.menulist = res.data;
 
        },

        // 点击按钮 折叠展开
        toggleCollapse(){
              this.isCollapse = !this.isCollapse;
        } ,
            //  保存链接的激活状态
        saveNavState(activePath){
                 window.sessionStorage.setItem('activePath',activePath);
                 this.activePath = activePath;
            }


    },
  
}

</script>


<style  lang="less"  scoped>
 .home-container{
     height: 100%;
 }
   .el-header{
       display: flex;
       justify-content:space-between;
       background-color: #373d41;
       padding-left: 0;
       align-items: center;
       color: #fff;
        div{
            display: flex;
            align-items: center;
           span{
           padding-left: 15px;
       }
       }   

   }
   .el-aside{
       background-color: #333744;
       .el-menu{
        //  么有这属性 菜单点出来的时候可能有点点对不齐
         border-right: none;
       }
   }
   .el-main{
       background-color: #EAEDF1;
   }
   .iconfont{
     padding-right: 8px;
   }
   .toggle-button{
     background-color: #4A5064;
     font-size: 10px;
     line-height: 24px;
     color: #fff;
     text-align: center;
     letter-spacing: 0.2em;
     cursor: pointer;
     a{
        color: #fff;
        text-decoration: none;
     }
   }
</style>